/*
Copyright (c) 2010 Neil Wood

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package envisage.Controls
{
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	public class Grid extends List
	{
		public function Grid()
		{
			super();
			
		
		}
		
		private var itemSize2:Number = 20;
		public function get ItemSize2():Number	{ return itemSize2; }
		public function set ItemSize2(value:Number):void { if( itemSize2 != value) { itemSize2 = value; RequireLayout(); } }
		
		private var innerPadding2:Number = 0;
		public function get InnerPadding2():Number { return innerPadding2; }
		public function set InnerPadding2(value:Number):void { if( innerPadding2 != value) { innerPadding2 = value; RequireLayout(); } }
		
		private var outerPadding2:Number = 0;
		public function get OuterPadding2():Number { return outerPadding2; }
		public function set OuterPadding2(value:Number):void { if( outerPadding2 != value) { outerPadding2 = value; RequireLayout(); } }
		
		public function get ItemsOpposing():int
		{
			if( Orient == Orientation.VERT ) return Math.max(1, ((outerPadding2 * 2) + (ClientWidth-innerPadding2)) / (itemSize2 + innerPadding2) ); 
			else						  return Math.max(1, ((outerPadding2 * 2) + (ClientHeight-innerPadding2)) / (itemSize2 + innerPadding2) );
		}
		
		protected override function LayoutScrollbar(dataLen:int, total:Number):void
		{
			dataLen = Math.ceil(dataLen / ItemsOpposing);
			total   = (dataLen * ItemSize) + (OuterPadding * 2) + (Math.max(dataLen-1, 0) * InnerPadding);
			
			super.LayoutScrollbar(dataLen, total);
		}
		
		protected override function LayoutVisibleItems(dataLen:int, vert:Boolean):void
		{	
			var io:int = ItemsOpposing;
			
			var newTop:int = (Offset-OuterPadding) / (ItemSize + InnerPadding);
			newTop *= io;
			
	
				 if( newTop < 0 ) newTop = 0;
			else if( newTop > dataLen ) newTop = dataLen;
			
			var amt:Number = vert ? ClientHeight : ClientWidth;
			var newBot:int = (newTop + Math.ceil(amt / (ItemSize+InnerPadding) *io ))+io+1;
			if( newBot > dataLen ) newBot = dataLen;
			if( newBot < newTop ) newBot = newTop;
			
			uiArraySubset.SetVisibleItemsTopIndex(newTop);
			uiArraySubset.SetVisibleItemsLength(newBot-newTop);
			
			if( vert ) LayoutChildren(borderLeft, (-(Offset % (ItemSize+InnerPadding))) + borderTop + OuterPadding, (ItemSize2 + InnerPadding2), (ItemSize + InnerPadding), borderLeft + OuterPadding2 + (ItemsOpposing*(ItemSize2+InnerPadding2)), Number.POSITIVE_INFINITY);
			else 	   LayoutChildren((-(Offset % (ItemSize+InnerPadding))) + borderLeft + OuterPadding, borderTop, (ItemSize+InnerPadding), (ItemSize2+InnerPadding2), Number.POSITIVE_INFINITY, 0);
		}
		
		public override function IndexFromPoint(p:Point):int
		{
			if( p.x < borderLeft ) return -1;
			if( p.y < borderTop  ) return -1;
			
			var i:Number;
			
			if( Orient == Orientation.VERT )
			{
				if( Scrollbar == null && p.x > width - borderRight ) return -1;
				if( Scrollbar != null && p.x > width - (borderRight + _Scrollbar.width) ) return -1;
				p.y -= borderTop;
			}
			
			else
			{
				if( Scrollbar == null && p.y > height - borderBottom ) return -1;
				if( Scrollbar != null && p.y > height - (borderBottom + _Scrollbar.height) ) return -1;
				p.x -= borderLeft;	
			}
			
			var col:int;
			var row:int;
			var index:int;
			
			if( Orient == Orientation.VERT )
			{
				p.y -= OuterPadding;
				p.y += (Offset % (ItemSize+InnerPadding));
				p.y /= (ItemSize+InnerPadding);
				
				p.x -= OuterPadding2;
				p.x += (0 % (ItemSize2 + InnerPadding2));
				p.x /= (ItemSize2+InnerPadding2);
				
				row = p.y + uiArraySubset.GetVisibleItemTopIndex() / ItemsOpposing;
				col = p.x;
				
				index = ( row * ItemsOpposing) + col;
			}
			else
			{
				p.x -= OuterPadding;
				p.x += (Offset % (ItemSize+InnerPadding));
				p.x /= (ItemSize+InnerPadding);
				
				p.y -= OuterPadding2;
				p.y += (0 % (ItemSize2 + InnerPadding2));
				p.y /= (ItemSize2+InnerPadding2);
				
				col = p.x + uiArraySubset.GetVisibleItemTopIndex() / ItemsOpposing;
				row = p.y;
				
				index = ( row * ItemsOpposing) + col;
			}
			
			return index;
		}

	}
}


